package com.example.pkmedical.Utils;

import android.os.Handler;
import android.util.Log;

import com.kongqw.serialportlibrary.SerialPortManager;
import com.kongqw.serialportlibrary.listener.OnSerialPortDataListener;

import java.io.File;

import static com.example.pkmedical.Utils.NumberUtil.bytesToHexString;
import static com.example.pkmedical.Utils.NumberUtil.stringToBytes;


/**
 * created by dan
 */
public class SerialProtUtil {
    private static SerialProtUtil serialProtUtil;

    public static SerialProtUtil getInstance() {
        if (serialProtUtil == null) {
            synchronized (SerialProtUtil.class) {
                if (serialProtUtil == null) {
                    serialProtUtil = new SerialProtUtil();
                }
            }

        }
        return serialProtUtil;
    }

    private SerialProtUtil() {
        serialPortManager = new SerialPortManager();
        String dd = SPUtils.getInstance().getString("curCode");
        try {
            if (dd != null && !"".equals(dd)) {
                curCode = stringToBytes(dd);
            }
        } catch (Exception e) {

        }


    }

    private SerialPortManager serialPortManager;
    //    private static String filePath = "/dev/ttyAMA2";
    private static String filePath = "/dev/ttyS1";

    private static int bt = 115200;
    private OnSerialPortListener onSerialPortListener;
    private int code_type = -1;//指令类型 1单个指令 2指令集
    private byte[] curCode;
    private boolean isSend = false;
    private boolean isOpen = false;

    /**
     *
     */
    private int repetition = 2;
    private int repetitionCount = 0;


    public int getCode_type() {
        return code_type;
    }

    public int getRepetition() {
        return repetition;
    }

    public void setRepetition(int repetition) {
        this.repetition = repetition;
    }

    public int getRepetitionCount() {
        return repetitionCount;
    }

    public void setRepetitionCount(int repetitionCount) {
        this.repetitionCount = repetitionCount;
    }

    /**
     * 气垫控制
     */
    //常规模式
    public static byte[] normal = new byte[]{0x11, 0x01, (byte) 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF};
    //睡眠模式
    public static byte[] sleep = new byte[]{0x11, 0x01, (byte) 0x82, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF};
    //左翻模式
    public static byte[] left = new byte[]{0x11, 0x01, (byte) 0x85, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF};
    //右翻模式
    public static byte[] right = new byte[]{0x11, 0x01, (byte) 0x86, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF};
    //最大充气
    public static byte[] max = new byte[]{0x11, 0x01, (byte) 0x83, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF};
    //屁股模式
    public static byte[] pigu = new byte[]{0x11, 0x01, (byte) 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF};
    //opt模式
    public static byte[] opti = new byte[]{0x11, 0x01, (byte) 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF};
    //舒适模式
    public static byte[] comfor = new byte[]{0x11, 0x01, (byte) 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF};
    //平躺
    public static byte[] liedown = new byte[]{0x11, 0x01, (byte) 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF};
    //敲击震动
    public static byte[] pupu = new byte[]{0x11, 0x01, (byte) 0x8A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF};
    //心跳
    public static byte[] xintiao = new byte[]{0x11, 0x03, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF};

    //哔哔哔响报警
    public static byte[] bbbAlert = new byte[]{0x011, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF};
    //停止报警
    public static byte[] stopAlert = new byte[]{0x011, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF};
    //持续三秒报警
    public static byte[] threeSecondAlert = new byte[]{0x011, 0x04, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF};

    //开始发送
    public static byte[] sendCode_code = new byte[]{0x011, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF};

    //有人
    public static byte[] have_r = new byte[]{0x011, 0x02, 0x07, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF};

    //无人
    public static byte[] have_nr = new byte[]{0x011, 0x02, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF};
    private static boolean isOpenSerialPort;

    public void setOnSerialPortListener(OnSerialPortListener onSerialPortListener) {
        this.onSerialPortListener = onSerialPortListener;
    }

    public byte[] getCurCode() {
        return curCode;
    }

    /**
     * 打开串口通信
     */
    public void openSerialPort() {
        isOpenSerialPort = serialPortManager.openSerialPort(new File(filePath), bt);
//       onSerialPortListener.onOpenSerialPort(isOpenSerialPort);
    }

    /**
     * 关闭串口通信
     */
    public void closeSerialPort() {
        if (serialPortManager != null) {
            serialPortManager.closeSerialPort();
        }
    }

    /**
     * 数据返回监听
     */
    public void setOnSerialPortDataListener(OnSerialPortDataListener onSerialPortDataListener) {
        serialPortManager.setOnSerialPortDataListener(onSerialPortDataListener);
    }

    Handler handler = new Handler();

    /**
     * 发送数据
     *
     * @param m_data
     */
    public void sendBytes(final int m_codeType, final byte[] m_data) {
//        if (CommandUtil.IS_CAN_REQUEST) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                code_type = m_codeType;
                curCode = m_data;

                String dd = bytesToHexString(m_data);
                SPUtils.getInstance().put("curCode", dd);
                Log.e("发送数据", m_data.toString());
                serialPortManager.sendBytes(m_data);
            }
        }, 100);
//        }else {
//            Log.e("yangshuoRequestError", m_data.toString());
//        }
    }

    /**
     * 发送数据
     *
     * @param m_data
     */
    public void sendBytes50(final int m_codeType, final byte[] m_data) {
//        if (CommandUtil.IS_CAN_REQUEST) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                code_type = m_codeType;
                curCode = m_data;

                String dd = bytesToHexString(m_data);
                SPUtils.getInstance().put("curCode", dd);
                Log.e("发送数据", m_data.toString());
                serialPortManager.sendBytes(m_data);
            }
        }, 50);
//        }else {
//            Log.e("yangshuoRequestError", m_data.toString());
//        }
    }

    /**
     * 发送数据
     *
     * @param m_data
     */
    public void sendBytes200(final int m_codeType, final byte[] m_data) {
//        if (CommandUtil.IS_CAN_REQUEST) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                code_type = m_codeType;
                curCode = m_data;
                String dd = bytesToHexString(m_data);
                SPUtils.getInstance().put("curCode", dd);
                Log.e("发送数据", m_data.toString());
                serialPortManager.sendBytes(m_data);
            }
        }, 200);
//        }else {
//            Log.e("yangshuoRequestError", m_data.toString());
//        }
    }

    public void sendBytes300(final int m_codeType, final byte[] m_data) {
//        if (CommandUtil.IS_CAN_REQUEST) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                code_type = m_codeType;
                curCode = m_data;

                String dd = bytesToHexString(m_data);
                SPUtils.getInstance().put("curCode", dd);
                Log.e("发送数据", m_data.toString());
                serialPortManager.sendBytes(m_data);
            }
        }, 300);
//        }else {
//            Log.e("yangshuoRequestError", m_data.toString());
//        }
    }

    /**
     * 发送数据
     *
     * @param m_data
     */
    public void sendBytes400(final int m_codeType, final byte[] m_data) {
//        if (CommandUtil.IS_CAN_REQUEST) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                code_type = m_codeType;
                curCode = m_data;

                String dd = bytesToHexString(m_data);
                SPUtils.getInstance().put("curCode", dd);
                Log.e("发送数据", m_data.toString());
                serialPortManager.sendBytes(m_data);
            }
        }, 400);
//        }else {
//            Log.e("yangshuoRequestError", m_data.toString());
//        }
    }
    /**
     * 发送数据
     *
     * @param m_data
     */
    public void sendBytes500(final int m_codeType, final byte[] m_data) {
//        if (CommandUtil.IS_CAN_REQUEST) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                code_type = m_codeType;
                curCode = m_data;

                String dd = bytesToHexString(m_data);
                SPUtils.getInstance().put("curCode", dd);
                Log.e("发送数据", m_data.toString());
                serialPortManager.sendBytes(m_data);
            }
        }, 500);
//        }else {
//            Log.e("yangshuoRequestError", m_data.toString());
//        }
    }


    public void sendBytes() {
        serialPortManager.sendBytes(curCode);
    }

    public void sendBytes2() {
        serialPortManager.sendBytes(xintiao);
    }

    public void setSend(boolean send) {
        isSend = send;
    }

    public boolean isSend() {
        return isSend;
    }

    public interface OnSerialPortListener {
        void onOpenSerialPort(boolean isOpen);

    }

}
